//@原文链接:
//https://blog.csdn.net/ITSOK_123/article/details/124228455
//朴素模拟
class Solution {
public:
    string reverseStr(string s, int k) {
        //初始化返回字符串、保存长度
        string s1 =s;
        int len=s1.size();
        //一次遍历循环，i每次前进2k步
        for(int i=0;i<len;i+=2*k){
            //交换局部字串操作
            int left,right;
            //待交换部分长度大于k
            if(len-i>k){
                left=i,right=i+k-1;
            }else{
                //最后的待交换部分长度小于k
                left=i,right=len-1;
            }
            //子串倒置
            while(left<right){
                    swap(s1[right],s1[left]);
                    left++;
                    right--;
                }
            
        }
        return s1;
    }
};

//库函数模拟
string reverseStr(string s, int k) {
        int n = s.length();
        for (int i = 0; i < n; i += 2 * k) {
            reverse(s.begin() + i, s.begin() + min(i + k, n));
        }
        return s;
    }
